/**
 *  @file SDLResourceManager.h
 *
 *  @date 25-nov-2009
 *  @author Ives van der Flaas
 */

#ifndef SDLRESOURCEMANAGER_H_
#define SDLRESOURCEMANAGER_H_

#include <SDL_ttf.h>
#include <SDL.h>
#include <SDL_mixer.h>
#include <string>
#include <map>
#include <list>
#include <tr1/memory>
#include "resourceManager.h"
#include "SDLColor.h"
#include "SDLPrefetchNode.h"

namespace si_sdl
{
/**
 * Singleton class that manages all SDL resources...
 * This includes Fonts and SDL_Surfaces
 */
class SDLResourceManager: public si::ResourceManager
{
public:
	static SDLResourceManager& get()
	{
		static SDLResourceManager mgr;
		return mgr;
	}

	/**
	 * Gets a font resource used to draw text. A font is coupled to a certain size.
	 * @param fontLocation Where i can find the path
	 * @param sizeInPt The size you would like
	 * @return
	 */
	std::tr1::shared_ptr<TTF_Font> getFont(std::string fontLocation, int sizeInPt);

	/**
	 * Loads the image into memory and sends you a COPY. This copy is yours. It is independent.
	 * Change it. Do whatever you like with it. As soon as the shared_ptr you get goes out
	 * of scope, your version of the image is gone and gone forever.
	 * @param location Location of the image to be loaded
	 * @param c Colorkey for transparancy
	 * @return shared_ptr with copy of image in it...
	 */
	std::tr1::shared_ptr<SDL_Surface> getImage(std::string location, Color c);

	/**
	 * Loads the image into memory and sends you a COPY. This copy is yours. It is independent.
	 * Change it. Do whatever you like with it. As soon as the shared_ptr you get goes out
	 * of scope, your version of the image is gone and gone forever.
	 * @param location Location of the image to be loaded
	 * @return shared_ptr with copy of image in it...
	 */
	std::tr1::shared_ptr<SDL_Surface> getImageAlpha(std::string location);

	/**
	 * Will load mp3, ogg, etc files...
	 * @param location
	 * @return
	 */
	std::tr1::shared_ptr<Mix_Music> getMusic(std::string location);

	/**
	 * Gets a chunk of sound. Only wav!
	 * @param location
	 * @return
	 */
	std::tr1::shared_ptr<Mix_Chunk> getChunk(std::string location);


	virtual ~SDLResourceManager();


	void prefetchNextTime(SDLPrefetchNode* prefetchNode);


private:
	std::string encode(std::string filename, int pts) const;
	std::string encodeImage(std::string filename, bool transparency) const;
	SDLResourceManager();
	std::map<std::string, std::tr1::shared_ptr<TTF_Font> > fFonts;
	std::map<std::string, std::tr1::shared_ptr<SDL_Surface> > fSurfaces;
	std::map<std::string, std::tr1::shared_ptr<Mix_Music> > fMusic;
	std::map<std::string, std::tr1::shared_ptr<Mix_Chunk> > fChunks;

	std::list<std::tr1::shared_ptr<SDLPrefetchNode> > fPrefetchNodes;
};
}

#endif /* SDLRESOURCEMANAGER_H_ */
